using System;
using System.Text;

namespace UrDnd.IrcBot.ComponentModel
{
	public class IRCDecoration
	{
		public static readonly string Bold = Char.ConvertFromUtf32(0x02);
		public static readonly string Color = Char.ConvertFromUtf32(0x03);
		public static readonly string NoDecoration = Char.ConvertFromUtf32(0x0F);
		public static readonly string Reverse = Char.ConvertFromUtf32(0x16);
		public static readonly string Underline = Char.ConvertFromUtf32(0x1F);
		private IRCColors? _background = null;

		private IrcDecorationEnum _decoration = IrcDecorationEnum.NoDecoration;
		private IRCColors? _foreground = null;

		public IRCDecoration() {}

		public IRCDecoration(IrcDecorationEnum decoration, IRCColors foreground, IRCColors background)
			: this(decoration, foreground)
		{
			_background = background;
		}

		public IRCDecoration(IrcDecorationEnum decoration, IRCColors foreground)
			: this(decoration)
		{
			_foreground = foreground;
		}

		public IRCDecoration(IrcDecorationEnum decoration)
		{
			_decoration = decoration;
		}

		public string OpenDecorationString
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				if ((_decoration & IrcDecorationEnum.Color) == IrcDecorationEnum.Color)
				{
					sb.Append(Color);
					if (_foreground.HasValue)
					{
						sb.AppendFormat("{0:2}", (int) _foreground.Value);

						if (_background.HasValue)
							sb.AppendFormat(",{0:2}", (int) _background.Value);
					}
				}

				if ((_decoration & IrcDecorationEnum.Bold) == IrcDecorationEnum.Bold)
					sb.Append(Bold);

				if ((_decoration & IrcDecorationEnum.Underline) == IrcDecorationEnum.Underline)
					sb.Append(Underline);

				if ((_decoration & IrcDecorationEnum.Reverse) == IrcDecorationEnum.Reverse)
					sb.Append(Reverse);

				if ((_decoration & IrcDecorationEnum.NoDecoration) == IrcDecorationEnum.NoDecoration)
					sb.Append(NoDecoration);

				return sb.ToString();
			}
		}

		public IrcDecorationEnum Decoration
		{
			get { return _decoration; }
			set { _decoration = value; }
		}

		public IRCColors? Foreground
		{
			get { return _foreground; }
			set { _foreground = value; }
		}

		public IRCColors? Background
		{
			get { return _background; }
			set { _background = value; }
		}

		public string CloseDecorationString
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				if ((_decoration & IrcDecorationEnum.Color) == IrcDecorationEnum.Color)
					sb.AppendFormat("{0}", Color);

				if ((_decoration & IrcDecorationEnum.Bold) == IrcDecorationEnum.Bold)
					sb.Append(Bold);

				if ((_decoration & IrcDecorationEnum.Underline) == IrcDecorationEnum.Underline)
					sb.Append(Underline);

				if ((_decoration & IrcDecorationEnum.Reverse) == IrcDecorationEnum.Reverse)
					sb.Append(Reverse);

				if ((_decoration & IrcDecorationEnum.NoDecoration) == IrcDecorationEnum.NoDecoration)
					sb.Append(NoDecoration);

				return sb.ToString();
			}
		}
	}
}